<template>
  <div class="app-container">
    <el-row :gutter="24" style="height: 100%;">
      <el-col :span="24">
        <el-button @click="back" >返回</el-button>
        <el-button type="primary" :loading='loading' @click="submitForm('itemModel')">保存</el-button>
      </el-col>
      <!-- 基本信息 -->
      <el-col :span="24"  style="margin-top: 20px;padding: 20px 0;padding-bottom: 50px;border: 1px solid #eee;">
        <base-info ref="baseinfo" :itemModel='itemModel'></base-info>
      </el-col>
      <!-- 商品详情区 -->
      <el-col :span="24" style="margin-top: 20px;margin-left: 20px;padding: 0 20px;border: 1px solid #eee;">
        <cover-details ref="details" :itemModel='itemModel'></cover-details>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { createItem, updateItem, getItemInfo } from "@/api/merchant/item.js"
import baseInfo from '@/views/merchant/item/createItem/components/baseInfo'
import coverDetails from '@/views/merchant/item/createItem/components/coverDetails'
export default {
  components: {baseInfo, coverDetails},
  data() {
    return {
      itemId: '',
      itemModel:{status: 1, payType: 1, isCheckStock: 1, isSingleSpec: 0, expenseSendIncomesType:1, directSendIncomesType:1, skuSpecList: [], skuList:[], specList: [], cateModel: {}, coverPhotoList:[]},
      loading: false,
      // currentPage: ''
    }
  },
  created(){
    if(this.$route.query.itemId){
    //   var itemModel = JSON.parse(this.$route.query.itemModel)
    //   //重组coverPhotos
    //   var coverPhotoList = []
    //   for(var item of JSON.parse(itemModel.coverPhotos)){
    //     var response = {data:{}}
    //     response.data.url = item.url
    //     coverPhotoList.push({url: item.url, response: response, name: ''})
    //   }
    //   console.log('coverPhotoList', coverPhotoList)
    //   itemModel.coverPhotoList = coverPhotoList
      
    //   this.itemModel = itemModel
      this.itemId = this.$route.query.itemId
      this.getItemInfo()
    }
    // if(this.$route.query.currentPage){
    //   this.currentPage = this.$route.query.currentSearchParams
    // }
  },
  methods:{
    
    back(){
      this.$router.push({name: this.$route.query.currentRouteName, query: {currentSearchParams: this.$route.query.currentSearchParams}})
    },
    //表单提交
    submitForm(formName) {
      if(this.$refs['baseinfo'].validateForm() && this.$refs['details'].validateForm()){
        if(this.itemModel.id){
            this.updateItem('更新成功', this.itemModel)
          }else{
            this.createItem('新增成功', this.itemModel)
          }
      }
    },
    startLoading(){
      this.loading = true
    },
    endLoading(){
      this.loading = false
    },
    createItem(message, itemModel){
      this.startLoading()
      createItem(itemModel).then(res => {
        this.endLoading()
        if(res.data.code == 10000){
          // this.$router.go(-1)
          this.$router.push({name: this.$route.query.currentRouteName})
          this.$message({
            message: message,
            type: 'success'
          })
        }
      });
    },
    updateItem(message, itemModel){
      this.startLoading()
      itemModel.itemId = itemModel.id
      updateItem(itemModel).then(res => {
        this.endLoading()
        if(res.data.code == 10000){
          this.$router.push({name: this.$route.query.currentRouteName, query: {currentSearchParams: this.$route.query.currentSearchParams}})
          this.$message({
            message: message,
            type: 'success'
          })
        }
      });
    },
    async getItemInfo(){
      getItemInfo({obj_id: this.itemId}).then(res=>{
        if(res.data.code==10000){
          var itemModel = res.data.data
          // if(itemModel.activity){
          //   await this.request_post('pay/order/countProfit/', {skuList: itemModel.skuList, activityId: itemModel.activity}).then(res => {
          //     if (res.data.code == 10000) {
          //       this.skuList = res.data.data.skuList
          //     }
          //   });
          // }
          

          
          this.itemModel.activityKey = itemModel.activityModel.key
          this.itemModel = itemModel
          var skuSpecList = []
          if(itemModel.skuList.length>0){
            for(var item of itemModel.skuList){
              item.specList = JSON.parse(item.specValues)
            }
            for(var spec of itemModel.skuList[0].specList){
              skuSpecList.push(spec.specName)
            }
          }
          
          this.$refs.baseinfo.skuSpecList = skuSpecList
          this.$refs.baseinfo.skuList = itemModel.skuList
          this.$refs.baseinfo.skuList_old = JSON.parse(JSON.stringify(itemModel.skuList))
          this.$refs.baseinfo.findSpecList(itemModel.skuList)
          this.$refs.baseinfo.activityChange(itemModel.activity)

          var coverPhotoList = []
          for(var item of JSON.parse(itemModel.coverPhotos)){
            var response = {data:{}}
            response.data.url = item
            coverPhotoList.push({url: item, response: response, name: item})
          }
          this.$set(this.itemModel, 'coverPhotoList', coverPhotoList)
          }
      })
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
    },
  },
}
</script>

<style lang="scss" scoped>

.content{
  padding: 30px;
}
.cate_form{
  display: flex;
  flex-direction: row;
}
</style>
